using System;
using System.Collections.Generic;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Grid;
using E4RPDevExpress;
using ZecaFil.DataSet;

namespace ZecaFil.Controls
{
    public partial class WorkOrdersXtraUserControl : XtraUserControl, IGridView
    {

        private Controller _controller;

        #region Properties

        public Controller Controller
        {
            set
            {
                _controller = value;
                _controller.WorkOrderSaved += delegate { SetGridDataSource(); };
                SetGridDataSource();
            }
        }

        #endregion

        public WorkOrdersXtraUserControl()
        {
            InitializeComponent();
            gridView.DoubleClick += GridViewDoubleClick;
            DevExpressHelper.SetColumnAsCurrency(colValor);
            DevExpressHelper.SetColumnAsInteger(colExecutado);
            DevExpressHelper.SetColumnAsInteger(colReclamacao);
            DevExpressHelper.SetColumnAsInteger(colMarcado);
            DevExpressHelper.SetColumnAsPercentage(colProdutividade);
            DevExpressHelper.SetColumnAsInteger(colPago);
            DevExpressHelper.SetColumnAsCurrency(colValorPago);
        }

        #region Private Methods

        private void SetGridDataSource()
        {            
            IntervencoesDataSet dataSet = _controller.GetWorkOrders();
            gridControl.DataSource = null;
            gridControl.DataSource = dataSet;
            gridControl.DataMember = dataSet.Intervencao.TableName;
            DevExpressHelper.ShowGroupSummaryAsCurrency(gridView, new List<GridColumn> { colValor, colValorPago });
            DevExpressHelper.ShowGroupSummaryAsInteger(gridView, new List<GridColumn> { colExecutado, colReclamacao, colMarcado, colPago });
            DevExpressHelper.ShowGroupAverageAsPercentage(gridView, new List<GridColumn> { colProdutividade});
            
        }

        private void ShowDocument(long documentId)
        {
            var window = new WorkOrderXtraForm { DocumentId = documentId, Controller = _controller };
            window.ShowDialog();
        }

        #endregion

        public void AddDocument()
        {
            ShowDocument(-1);
        }

        public void PrintDocument()
        {
            var document = (IntervencoesDataSet.IntervencaoRow)gridView.GetDataRow(gridView.FocusedRowHandle);
            if (document == null)
                return;
            _controller.PrintWorkOrder(document.Id);
        }

        public GridView GridView()
        {
            return gridView;
        }

        public string ListName
        {
            get { return "WorkOrdersXtraUserControl"; }
        }

        void GridViewDoubleClick(object sender, EventArgs e)
        {
            var document = (IntervencoesDataSet.IntervencaoRow)gridView.GetDataRow(gridView.FocusedRowHandle);
            if (document == null)
                return;
            ShowDocument(document.Id);
        }
        
    }
}
